测试RC522刷卡模块
#include "sys.h"#include "delay.h"#include "usart.h"#include "led.h"#include "beep.h"#include "key.h"#include "rc522_config.h"#include "rc522_function.h"#include <stdbool.h>#define spacenum 20uint8_t KeyValue[]={0xFF ,0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; // 卡A密钥unsigned char Block=0;unsigned char Modify_Key[6]={0xff,0x00,0xff,0x00,0xff,0x00};//修改后的密码unsigned char Default_Key[6]={0xff,0xff,0xff,0xff,0xff,0xff};//初始密码unsigned char Write_Buffer[16]={//修改密码A0xff,0x00,0xff,0x00,0xff,0x00,//KEYA0xFF,0x07,0x80,0x69, //控制0xFF,0xFF,0xFF,0xFF,0xFF,0xFF //KEYB};unsigned char Write_Buffer1[16]={//还原密码A0xff,0xff,0xff,0xff,0xff,0xff,//KEYA0xFF,0x07,0x80,0x69,//控制0xFF,0xFF,0xFF,0xFF,0xFF,0xFF//KEYB};unsigned char Data_Buffer[16];//读块数据缓存void ModifyKey(u8 Block);//修改扇区1的第三块控制块的KEY Avoid RecoveryKey(u8 Block);//恢复扇区1的KEY A //修改扇区1的第0块控制块的KEY Avoid ModifyKey(u8 Block){u8 block=Block; uint8_t ucArray_ID [ 4 ]; /*先后存放IC卡的类型和UID(IC卡序列号)*/uint8_t ucStatusReturn; /*返回状态*//*寻卡*/if ( ( ucStatusReturn = PcdRequest ( PICC_REQIDL, ucArray_ID ) ) != MI_OK ) /*若失败再次寻卡*/ucStatusReturn = PcdRequest ( PICC_REQIDL, ucArray_ID ); if ( ucStatusReturn == MI_OK ){ /*防冲撞(当有多张卡进入读写器操作范围时,防冲突机制会从其中选择一张进行操作)*/if ( PcdAnticoll ( ucArray_ID ) == MI_OK ) {PcdSelect(ucArray_ID);if(PcdAuthState( PICC_AUTHENT1A, block, Default_Key, ucArray_ID )==MI_OK)//校验初始密码{ printf("验证秘钥成功!Card_Block: %2d\r\n",Block);delay_ms(100);if(PcdRead(block,Data_Buffer)==MI_OK){printf("%*s读取块 %02d 成功!Block_Info: %02X%02X%02X%02X%02X%02X %02X%02X%02X%02X %02X%02X%02X%02X%02X%02X \r\n",spacenum,"",block,Data_Buffer[0],Data_Buffer[1],Data_Buffer[2],Data_Buffer[3],Data_Buffer[4],Data_Buffer[5],Data_Buffer[6],Data_Buffer[7],Data_Buffer[8],Data_Buffer[9],Data_Buffer[10],Data_Buffer[11],Data_Buffer[12],Data_Buffer[13],Data_Buffer[14],Data_Buffer[15]);}if(PcdWrite(block,Write_Buffer)==MI_OK) {printf("密码修改成功!");}else{printf("密码修改失败!");}}else{printf("密码验证失败!");}}PcdHalt(); return;}}//恢复扇区1的KEY A 注销该卡void RecoveryKey(u8 Block){u8 block=Block; uint8_t ucArray_ID [ 4 ]; /*先后存放IC卡的类型和UID(IC卡序列号)*/uint8_t ucStatusReturn; /*返回状态*//*寻卡*/if ( ( ucStatusReturn = PcdRequest ( PICC_REQIDL, ucArray_ID ) ) != MI_OK ) /*若失败再次寻卡*/ucStatusReturn = PcdRequest ( PICC_REQIDL, ucArray_ID ); if ( ucStatusReturn == MI_OK ){ /*防冲撞(当有多张卡进入读写器操作范围时,防冲突机制会从其中选择一张进行操作)*/if ( PcdAnticoll ( ucArray_ID ) == MI_OK ) {PcdSelect(ucArray_ID);if(PcdAuthState( PICC_AUTHENT1A, block, Modify_Key, ucArray_ID )==MI_OK)//校验初始密码{ printf("验证秘钥成功!Card_Block: %2d\r\n",Block);delay_ms(100);if(PcdRead(block,Data_Buffer)==MI_OK){printf("%*s读取块 %02d 成功!Block_Info: %02X%02X%02X%02X%02X%02X %02X%02X%02X%02X %02X%02X%02X%02X%02X%02X \r\n",spacenum,"",block,Data_Buffer[0],Data_Buffer[1],Data_Buffer[2],Data_Buffer[3],Data_Buffer[4],Data_Buffer[5],Data_Buffer[6],Data_Buffer[7],Data_Buffer[8],Data_Buffer[9],Data_Buffer[10],Data_Buffer[11],Data_Buffer[12],Data_Buffer[13],Data_Buffer[14],Data_Buffer[15]);}if(PcdWrite(block,Write_Buffer1)==MI_OK){printf("密码还原成功!");}else{printf("密码还原失败!");}}else{printf("密码验证失败!");}}PcdHalt(); return;}}void IC_test ( void ){uint32_t writeValue = 100;uint32_t readValue;char cStr [ 30 ]; uint8_t ucArray_ID [ 4 ]; /*先后存放IC卡的类型和UID(IC卡序列号)*/uint8_t ucStatusReturn; /*返回状态*/ u8 sta;u8 sta2; u8 sum;u8 t;u8 read_result; /*寻卡*/if ( ( ucStatusReturn = PcdRequest ( PICC_REQIDL, ucArray_ID ) ) != MI_OK ) /*若失败再次寻卡*/ucStatusReturn = PcdRequest ( PICC_REQIDL, ucArray_ID ); if ( ucStatusReturn == MI_OK ){ /*防冲撞(当有多张卡进入读写器操作范围时,防冲突机制会从其中选择一张进行操作)*/if ( PcdAnticoll ( ucArray_ID ) == MI_OK ) {PcdSelect(ucArray_ID);for(t=1;t<64;t ){sta=PcdAuthState( PICC_AUTHENT1A, t, KeyValue, ucArray_ID );//校验密码if(sta==MI_OK){printf ( "第%d个块的密码校验成功,返回值:%X\r\n",t,sta); //打印密码校验结果sta=0;if((t%4)!=3){WriteAmount(t,t); //写入金额printf ( "第%d个块写入:金额%d元\r\n",t,t);}read_result=ReadAmount(t,&readValue);printf ( "第%d个块读取函数返回值:%X\r\n",t,read_result);if((t%4)!=3){ sprintf ( cStr, "TThe residual amount: %d", readValue);printf ("第%d块余额为:%d\r\n",t,readValue);}delay_ms(1000);if(t>=63){t=0;}}else{printf ( "第%x个块的密码校验失败,返回值:%X\r\n",t,sta); }}PcdHalt();return; // sta2=WriteAmount(0x11,writeValue); //写入金额//printf ( "%x\r\n",sta2); //打印写入函数返回值 /*sta2=ReadAmount(0x11,&readValue); if(sta2== MI_OK)//读取金额{//writeValue =100; sprintf ( cStr, "The Card ID is: %02X%02X%02X%02X",ucArray_ID [0], ucArray_ID [1], ucArray_ID [2],ucArray_ID [3] );printf ( "%s\r\n",cStr ); //打印卡片ID*/ /*sprintf ( cStr, "TThe residual amount: %d", readValue);printf ("余额为:%d\r\n",readValue); PcdHalt();return;}*/} }}int main(void){ u8 key;u16 times=0; NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//设置系统中断优先级分组2delay_init(168);//延时初始化 uart_init(115200);//串口初始化波特率为115200LED_Init(); //初始化与LED连接的硬件接口 KEY_Init();RC522_Init (); //RC522模块所需外设的初始化配置printf ( "WF-RC522 Test\r\n" );PcdReset ();M500PcdConfigISOType ( 'A' );//设置工作方式while(1){key=KEY_Scan(0);//printf("%d\r\n",key);// IC_test ();//IC卡检测if(key==KEY0_PRES){printf("KEY0按下\r\n");ModifyKey(7);}if(key==KEY1_PRES){printf("KEY1按下\r\n");RecoveryKey(7);}times ;if(times%30==0)LED0=!LED0;//闪烁LED,提示系统正在运行.delay_ms(10); }}
评论